﻿Public Class DimForm

    Private _Dim As Core.Dimensions.MetisDimension

    Public Event ErrorOccurred(ByVal Message As String)

    Sub New(ByVal Dimension As Core.Dimensions.MetisDimension)
        InitializeComponent()
        _Dim = Dimension


    End Sub

    Private Sub DimForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If _Dim.LoadState = Core.MetisLoadState.NewDefinition Then
            tcMain.SelectTab(tpFieldDef)
            btnReload.Enabled = False
        Else
            ReloadData()
        End If
        dgvFieldDef.AutoGenerateColumns = False
        ReloadDefinition()
    End Sub

    Public ReadOnly Property Dimension() As Core.Dimensions.MetisDimension
        Get
            Return _Dim
        End Get
    End Property

    Private Sub FormatFieldDefDGV()
        Try
            dgvFieldDef.SuspendLayout()
            For Each row As DataGridViewRow In dgvFieldDef.Rows
                Dim tmpField As Core.Dimensions.MetisDimensionField = row.DataBoundItem
                For Each cell As DataGridViewCell In row.Cells
                    cell.ReadOnly = False
                    cell.Style.BackColor = SystemColors.Window
                    cell.ToolTipText = ""
                Next

                If tmpField.FieldType <> Core.Dimensions.MetisDimensionFieldType.ValueField Then
                    row.Cells(colDBFieldName.Index).ReadOnly = True
                    row.Cells(colDBFieldName.Index).Style.BackColor = SystemColors.Control
                    row.Cells(colDBFieldType.Index).ReadOnly = True
                    row.Cells(colDBFieldType.Index).Style.BackColor = SystemColors.Control
                    row.Cells(colFieldDefault.Index).ReadOnly = True
                    row.Cells(colFieldDefault.Index).Style.BackColor = SystemColors.Control
                    row.Cells(colFieldLength.Index).ReadOnly = True
                    row.Cells(colFieldLength.Index).Style.BackColor = SystemColors.Control
                    row.Cells(colFieldNullable.Index).ReadOnly = True
                    row.Cells(colFieldNullable.Index).Style.BackColor = SystemColors.Control
                Else
                    If (tmpField.DBfieldType = Core.DB.DBFieldType.DBdate) Then ' OrElse (tmpField.DBfieldType = Core.DB.DBFieldType.DBnumber) Then
                        row.Cells(colFieldLength.Index).ReadOnly = True
                        row.Cells(colFieldLength.Index).Style.BackColor = SystemColors.Control
                    End If
                End If
            Next
        Finally
            dgvFieldDef.ResumeLayout()
        End Try

    End Sub

    Private Function IsValidDefinition() As Boolean
        Dim tmpResult As Boolean = Core.MetisCore.IsValidFieldName(txtDimName.Text, 64)
        Return tmpResult
    End Function

    Private Sub ReloadDefinition()
        _Dim.Reload()
        Me.Text = _Dim.Name
        txtDimName.Text = _Dim.Name
        txtTableName.Text = _Dim.Table.TableName
        txtComment.Text = _Dim.Annotation

        dgvFieldDef.DataSource = _Dim.Fields
    End Sub

    Private Sub ReloadData()

    End Sub

    Private Sub dgvFieldDef_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgvFieldDef.DataBindingComplete
        FormatFieldDefDGV()
        btnSave.Enabled = _Dim.IsModified AndAlso IsValidDefinition()
    End Sub

    Private Sub txtDimName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDimName.TextChanged
        Try
            _Dim.Name = txtDimName.Text
            txtDimName.BackColor = SystemColors.Window
            txtDimName.ToolTipText = ""
            btnSave.Enabled = _Dim.IsModified
        Catch ex As Core.Exceptions.MetisNameException
            txtDimName.BackColor = Color.Salmon
            txtDimName.ToolTipText = ex.Message
            btnSave.Enabled = False
        End Try
    End Sub

    Private Sub dgvFieldDef_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFieldDef.CellEndEdit
        tsFieldDef.Enabled = True
        FormatFieldDefDGV()
        btnSave.Enabled = _Dim.IsModified AndAlso IsValidDefinition()
    End Sub

    Private Sub btnField_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnField_Add.Click
        _Dim.Fields.Add(New Core.Dimensions.MetisDimensionField(_Dim))
        'dgvFieldDef.DataSource = _Dim.Fields
        btnSave.Enabled = _Dim.IsModified AndAlso IsValidDefinition()
    End Sub

    Private Sub btnField_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnField_Del.Click
        _Dim.Fields.Remove(dgvFieldDef.CurrentRow.DataBoundItem)
        'dgvFieldDef.DataSource = _Dim.Fields
        btnSave.Enabled = _Dim.IsModified AndAlso IsValidDefinition()
    End Sub

    Private Sub txtComment_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtComment.TextChanged
        _Dim.Annotation = txtComment.Text
        btnSave.Enabled = _Dim.IsModified AndAlso IsValidDefinition()
    End Sub

    Private Sub dgvFieldDef_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFieldDef.RowEnter
        Dim tmpField As Core.Dimensions.MetisDimensionField = dgvFieldDef.Rows(e.RowIndex).DataBoundItem
        btnField_Del.Enabled = (tmpField.FieldType = Core.Dimensions.MetisDimensionFieldType.ValueField)
    End Sub

    Private Sub dgvFieldDef_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgvFieldDef.DataError
        If (e.ColumnIndex = colFieldName.Index) Or (e.ColumnIndex = colDBFieldName.Index) Or (e.ColumnIndex = colFieldLength.Index) Then
            If e.Exception IsNot Nothing Then RaiseEvent ErrorOccurred(e.Exception.Message)
            e.Cancel = True
        End If
    End Sub

    Private Sub dgvFieldDef_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvFieldDef.CellBeginEdit
        tsFieldDef.Enabled = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        _Dim.Save()
        Me.Text = _Dim.Name
        btnSave.Enabled = _Dim.IsModified AndAlso IsValidDefinition()
    End Sub

    Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReload.Click
        ReloadDefinition()
    End Sub

    Private Sub DimForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If _Dim.IsModified Then
            Select Case MsgBox(String.Format(My.Resources.FormCloseMsg_Dim.ToString, Me.Text), MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question)
                Case MsgBoxResult.Yes
                    _Dim.Save()
                    _Dim.SaveFormat()
                Case MsgBoxResult.Cancel
                    e.Cancel = True
            End Select
        Else
            If _Dim.LoadState <> Core.MetisLoadState.NewDefinition Then _Dim.SaveFormat()
        End If

    End Sub
End Class